<?php
namespace app\index\controller;
use app\index\controller\Base;
class Index extends Base
{
    public function index()
    {
      //总浏览量
      db('click')->where('id',1)->setInc('click');
      //当日浏览量
      $time = db('click')->where('update_time','> time','today')->max('update_time');
      if($time){
        db('click')->where('id', 3)->setInc('click');
      }else{
        db('click')->where('id', 3)->update(['click'=>1]);
      }
      //当日访客
      $res = db('visitor')->select();
      $remote = $_SERVER['REMOTE_ADDR'];
      if($res){
        $onetime = db('visitor')->where('create_time','> time','today')->max('create_time');
        if($onetime){
          //总访客数
          $tory = implode(db('click')->where('id',2)->field('tory')->find())*1;
          $sum = db('visitor')->count('ip');
          $he = $tory + $sum;
          db('click')->where('id',2)->update(['click'=>$he]);

          //当日访客
          $ip = db('visitor')->where('ip',$remote)->select();
          if(empty($ip)){
            db('visitor')->data(['ip'=>$remote])->insert();
          }
          $sum = db('visitor')->count('ip');
          db('click')->where('id',4)->update(['click'=>$sum]);
        }else{
          //总访客数
          $now = implode(db('click')->where('id',2)->field('click')->find())*1;
          $tor = db('click')->where('id',2)->update(['tory'=>$now]);
          db('click')->where('id',2)->update(['click'=>$now + 1]);

          //当日访客
          db('visitor')->where('id','>',0)->delete();
          db('visitor')->data(['ip'=>$remote])->insert();
          $sum = db('visitor')->count('ip');
          db('click')->where('id',4)->update(['click'=>$sum]);
        }
      }else{
        //当日访客
        db('visitor')->data(['ip'=>$remote])->insert();
        $sum = db('visitor')->count('ip');
        db('click')->where('id',4)->update(['click'=>$sum]);

        //总访客数
        $tory = implode(db('click')->where('id',2)->field('tory')->find())*1;
        $he = $tory + $sum;
        db('click')->where('id',2)->update(['click'=>$he]);
      }
      $link = db('link')->select();
      $baojie = db('cate')->where('pid',5)->select();
      $baomu = db('cate')->where('pid',15)->select();
      $banjia = db('cate')->where('pid',24)->select();
      $weixiu = db('cate')->where('pid',28)->select();
      $jiadian = db('cate')->where('pid',31)->select();
      $qiye = db('cate')->where('pid',34)->select();
      $news = db('article')->where('cateid','=',83)->select();
      $cate = db('cate')->where('pid',4)->select();
      $this->assign(array(
        'link' => $link,
        'baojie' => $baojie,
        'baomu' => $baomu,
        'banjia' => $banjia,
        'weixiu' => $weixiu,
        'jiadian' => $jiadian,
        'qiye' => $qiye,
        'news' => $news,
        'cate' => $cate
      ));
      return view();
    }

}